package com.shengqi.xinnongren.work.ui.home.fragments.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.DecelerateInterpolator;
import android.widget.HorizontalScrollView;
import android.widget.Scroller;

public class SlideHorizontalScrollView extends HorizontalScrollView {

    private boolean isSetChildWidth = false;
    private ViewGroup mLeftChild;
    private View mRightChild;

    private int mScreenWidth;
    private int mRightChildWidth;

    private Scroller mScroller;

    private OnMenuOpenStateChangeListener mOpenStateChangeListener;


    public SlideHorizontalScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
        mScreenWidth = metrics.widthPixels;
        mRightChildWidth = (int) (context.getResources().getDisplayMetrics().density * 90);
        setOverScrollMode(OVER_SCROLL_NEVER);
        mScroller = new Scroller(context, new DecelerateInterpolator());
        setHorizontalScrollBarEnabled(false);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (!isSetChildWidth) {
            ViewGroup child = ((ViewGroup) getChildAt(0));
            mLeftChild = (ViewGroup) child.getChildAt(0);
            mRightChild = child.getChildAt(1);
            mLeftChild.getLayoutParams().width = mScreenWidth;
            mRightChild.getLayoutParams().width = mRightChildWidth;
            isSetChildWidth = true;
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_UP:
                Log.i("-----------", "onTouchEvent: scrollX is " + getScrollX() + ", mRightChildWidth / 2 = " + (mRightChildWidth / 2));
                if (getScrollX() > mRightChildWidth / 2) {
                    openMenu();
                } else {
                    closeMenu();
                }
                break;
        }
        return super.onTouchEvent(ev);
    }

    public void setOpenStateChangeListener(OnMenuOpenStateChangeListener openStateChangeListener) {
        mOpenStateChangeListener = openStateChangeListener;
    }

    public void closeMenu() {
        mScroller.startScroll(getScrollX(), 0, 0 - getScrollX(), 0);
        invalidate();
        if (mOpenStateChangeListener != null) {
            mOpenStateChangeListener.onClose();
        }
    }

    public void openMenu() {
        mScroller.startScroll(getScrollX(), 0, mRightChildWidth - getScrollX(), 0);
        invalidate();
        if (mOpenStateChangeListener != null) {
            mOpenStateChangeListener.onOpen();
        }
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (mScroller.computeScrollOffset()) {
            scrollTo(mScroller.getCurrX(), 0);
        }
    }

    @Override
    public void fling(int velocityX) {
        super.fling(velocityX / 1000);
    }

    public interface OnMenuOpenStateChangeListener{
        void onOpen();
        void onClose();
    }
}
